package com.yingwei.mining.view;

import java.awt.BorderLayout; 
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.border.BevelBorder;

import com.yingwei.mining.control.Data;
import com.yingwei.mining.model.businessobject.Edge;
import com.yingwei.mining.model.businessobject.Excavator;
import com.yingwei.mining.model.businessobject.Node;
import com.yingwei.mining.model.businessobject.UnloadArea;
import com.yingwei.mining.util.LocationUtil;

abstract public class MapPanel extends JDesktopPane 
{
    private static final long serialVersionUID = 5045085225306142712L;

    Image backgroundImage;
    Image truckImage;
    Image excavatorImage;
    Image unloadAreaImage;
    boolean showLength;
    boolean showIcon;
    MapPanel self = this;

    int viewMode = 0;
    Image offscreen;
    Dimension offscreensize;
    Graphics offgraphics;

    final static Color fixedColor = Color.red;
    final static Color selectColor = Color.blue;
    final static Color edgeColor = Color.red;
    final static Color nodeColor = new Color(125, 122, 100);
    final static Color stressColor = Color.darkGray;
    final static Color arcColor1 = Color.black;
    final static Color arcColor2 = Color.pink;
    final static Color arcColor3 = Color.red;
    final static Color excavatorColor = Color.red;
    final static Color unloadAreaColor = Color.green;

    Data data = Data.getData();
    Node pickedNode = null;
    Edge pickedEdge = null;

    public MapPanel()
    {
	try
	{
	    backgroundImage = ImageIO.read(new File(
		    "C:\\workspace\\carmining\\image\\background.jpg"));
	    truckImage = ImageIO.read(new File(
		    "C:\\workspace\\carmining\\image\\truck.jpg"));
	    excavatorImage = ImageIO.read(new File(
		    "C:\\workspace\\carmining\\image\\excavator.jpg"));
	    unloadAreaImage = ImageIO.read(new File(
		    "C:\\workspace\\carmining\\image\\unloadarea.jpg"));
	}
	catch (IOException e)
	{
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}

	setBackground(Color.white);
	setOpaque(true);
	setPreferredSize(new Dimension(
		data.getConfiguration().getCalculatedScreenWidth(), 
		data.getConfiguration().getCalculatedScreenHeight()));
    }

    protected void paintComponent(Graphics g)
    {
	super.paintComponent(g);
	g.drawImage(backgroundImage, 0, 0, data.getConfiguration()
		.getCalculatedScreenWidth(), data.getConfiguration()
		.getCalculatedScreenHeight(), this);
	Iterator<Edge> eitr = data.getEdges().iterator();
	while (eitr.hasNext())
	{
	    paintEdge(g, eitr.next());
	}
	Iterator<Node> itr = data.getNodes().iterator();
	while (itr.hasNext())
	{
	    paintNode(g, itr.next());
	}
    }

    public void paintNode(Graphics g, Node n)
    {
	int x = LocationUtil.getScreenLocationX(n.getX());
	int y = LocationUtil.getScreenLocationY(n.getY());
	int dx = n.getDx();
	int dy = n.getDy();

	Image img = null;
	if (pickedNode != null && n.getId() == pickedNode.getId())
	{
	    g.setColor(selectColor);
	    if (n.getDevice() == null)
	    {

	    }
	    else if (n.getDevice() instanceof Excavator)
	    {
		img = excavatorImage;
	    }
	    else if (n.getDevice() instanceof UnloadArea)
	    {
		img = unloadAreaImage;
	    }
	}
	else
	{
	    if (n.getDevice() == null)
	    {
		g.setColor(nodeColor);
	    }
	    else if (n.getDevice() instanceof Excavator)
	    {
		g.setColor(excavatorColor);
		img = excavatorImage;
	    }
	    else if (n.getDevice() instanceof UnloadArea)
	    {
		g.setColor(unloadAreaColor);
		img = unloadAreaImage;
	    }
	    else
	    {
		System.out.println("not an instance of :" + n.getId() + ":");
		// + n.getDevice());
	    }
	}
	FontMetrics fm = g.getFontMetrics();
	int w = fm.stringWidth("" + n.getId()) + 10;
	int h = fm.getHeight() + 4;
	w = dx;
	h = dy;
	if (showIcon)
	{
	    if (img != null)
	    {
		g.drawImage(img, x, y, this);
	    }
	}
	else
	{
	    g.fillRect(x - w / 2, y - h / 2, w, h);
	    g.setColor(Color.black);
	    g.drawRect(x - w / 2, y - h / 2, w - 1, h - 1);
	}
	g.drawString("" + n.getId(), x - (w - 10) / 2, (y - (h - 4) / 2)
		+ fm.getAscent());
	if (img != null)
	{
	    g.drawImage(img, x - w / 2, y + h / 2, this);
	    g.drawString(n.getDevice().getName(),x - w / 2, y + 2*h );
	}
    }

    public void paintEdge(Graphics g, Edge e)
    {
	if (pickedEdge != null && e.getId() == pickedEdge.getId())
	{
	    g.setColor(selectColor);
	}
	else
	{
	    g.setColor(edgeColor);
	}
	int x1 = LocationUtil.getScreenLocationX(e.getFromNode().getX());
	int y1 = LocationUtil.getScreenLocationY(e.getFromNode().getY());
	int x2 = LocationUtil.getScreenLocationX(e.getToNode().getX());
	int y2 = LocationUtil.getScreenLocationY(e.getToNode().getY());
	g.drawLine(x1,y1,x2,y2);
	int dx = x2 - x1;
	int dy = y2 - y1;
	g.drawOval(x1+dx*6/9, y1+dy*6/9,5,5);
	g.drawOval(x1+dx*7/9, y1+dy*7/9,3,3);
    }

}
